/**
 * @param {number} num
 * @return {string}
 */
var intToRoman = function (num) {
  if(!num) {
    return '';
  }
  let map = {
    '1': 'I',
    '5': 'V',
    '10': 'X',
    '50': 'L',
    '100': 'C',
    '500': 'D',
    '1000': 'M'
  }
  let specialMap = {
    4: 'IV',
    9: 'IX',
    40: 'XL',
    90: 'XC',
    400: 'CD',
    900: 'CM',
  }
  let len = (num + '').length;
  let arr = String(num).split('');
  let i = 0;
  let prefix = ['M', 'C', 'X', 'I'];
  prefix = prefix.slice(4 - len);
  let str = [];
  while (i < len) {
    let pi = arr[i];
    // 补0
    let padpi = pi.padEnd(len - i, 0);
    if(!pi) {
      i++;
      continue
    }
    if(specialMap[padpi]) {
      str.push(specialMap[padpi]);
    } else if(map[padpi]) {
      str.push(map[padpi]);
    } else {
      // 要做分解
      let dif = pi - 5;
      let sub = '5'.padEnd(len - i, 0);
      let difpad = String(dif).padEnd(len - i, 0);
      // 当前位切换，高位的用就近的替换
      if(dif > 0) {
        str.push(map[sub] || specialMap[sub]);
      } else {
        dif = pi;
      }
      for (let j = 0; j < dif; j++) {
        str.push(prefix[i])
      }
      
    }
    i++;
  }
  console.log(str.join(''));
  return str.join('')
};

// intToRoman(3)
// intToRoman(4)
// intToRoman(5)
// intToRoman(6)
intToRoman(776)